package com.biz.primus.ms.order.service.bulider;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.model.order.exception.utils.AssertRequestParameter;
import com.biz.primus.model.order.vo.OrderConsigneeInfoVO;
import com.biz.primus.model.order.vo.response.OrderPromotionResponseVO;
import com.biz.primus.model.order.vo.response.OrderSummaryResponseVO;
import com.biz.primus.ms.order.convertor.OrderSummaryConvertor;
import com.biz.primus.ms.order.dao.po.OrderSummary;
import com.biz.primus.ms.order.service.helper.OrderServiceHelper;

/**
 * 创建订单构建器，负责创建订单的细节<br>
 * 获取促销、优惠券，锁库存，使用促销、优惠券
 *
 * @author Tiger Feng
 * @date 2017年11月02日
 */
public class CreateOrderBuilder {
    /**
     * 结算后/需要创建的订单
     */
    private OrderSummaryResponseVO orderSummaryResponseVO;
    /**
     * 订单服务助手，构造方法注入
     */
    private OrderServiceHelper helper;

    /*****************************************************
     * 临时变量
     */
    private OrderSummary orderSummary;
    /**
     * 收货地址信息
     */
    private OrderConsigneeInfoVO consignee;

    private CreateOrderBuilder() {
    }

    private CreateOrderBuilder(OrderSummaryResponseVO orderSummaryResponseVO, OrderServiceHelper helper) {
        this.orderSummaryResponseVO = orderSummaryResponseVO;
        this.helper = helper;
        this.consignee = orderSummaryResponseVO.getConsignee();
    }

    /**
     * 1、创建构建器
     *
     * @param orderSummaryResponseVO
     * @param helper
     * @return
     */
    public static CreateOrderBuilder newBuilder(OrderSummaryResponseVO orderSummaryResponseVO, OrderServiceHelper helper) {
        AssertUtils.notNull(helper, OrderException.ORDER_HELPER_CAN_NOT_NULL);
        AssertRequestParameter.notNull(orderSummaryResponseVO, "订单汇总响应模型不能为空");
        return new CreateOrderBuilder(orderSummaryResponseVO, helper);
    }

    /**
     * 2、保存订单，持久化订单
     *
     * @return
     */
    public CreateOrderBuilder saveOrder() {
        AssertRequestParameter.notNull(consignee, "收货地址信息不能为空");
        AssertRequestParameter.hasLength(consignee.getConsigneeName(), "收货人姓名不能为空");
        AssertRequestParameter.hasLength(consignee.getConsigneeMobile(), "收货人电话不能为空");
        AssertRequestParameter.notNull(consignee.getConsigneeProvinceId(), "收货地址省编号不能为空");
        AssertRequestParameter.notNull(consignee.getConsigneeCityId(), "收货地址城市编号不能为空");
        AssertRequestParameter.notNull(consignee.getConsigneeDistrictId(), "收货地址区县编号不能为空");
        AssertRequestParameter.hasLength(consignee.getConsigneeAddress(), "收货人详细地址不能为空");
        orderSummary = OrderSummaryConvertor.respVO2PO(orderSummaryResponseVO);
        orderSummary = helper.saveOrderSummary(orderSummary);
        return this;
    }

    /**
     * 3、锁定商品库存
     * 中台锁
     *
     * @return
     */
    public CreateOrderBuilder lockedInventory() {
        // 门店订单所库存,库存订单不处理
        if (orderSummary.getStoreOrder() != null) {
            helper.addLockStock(orderSummary.getStoreOrder());
        }
        return this;
    }

    /**
     * 4、使用促销
     */
    public CreateOrderBuilder usePromotion() {
        try {
            OrderPromotionResponseVO promotion = orderSummaryResponseVO.getPromotion();
            if (promotion != null) {
                helper.usePromotion(orderSummaryResponseVO.getUserId(), promotion.getId(),promotion.getPromotionType());
            }
        }
        // 使用促销需要捕获异常
        // 回退锁定商品库存
        catch (Exception e) {
            if (orderSummary.getStoreOrder() != null) {
                helper.subtractLockStock(orderSummary.getStoreOrder());
            }
            throw e;
        }
        return this;
    }

    /**
     * 5、使用优惠券
     */
    public CreateOrderBuilder useCoupon() {

        return this;
    }

    /**
     * 6、构建结果
     *
     * @return
     */
    public OrderSummaryResponseVO build() {
        return orderSummaryResponseVO;
    }
}
